package org.vineatoum.view;
import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JPanel;
import java.awt.BorderLayout;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.Iterator;

import javax.swing.JTextField;
import javax.swing.JLabel;
import javax.swing.JButton;
import javax.swing.JList;
import javax.swing.AbstractListModel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import org.vineatoum.bo.Plantation;

import javax.swing.JSplitPane;
import javax.swing.JToolBar;
import javax.swing.JTabbedPane;
import javax.swing.JDesktopPane;
import javax.swing.JScrollPane;
import javax.swing.SwingConstants;
import javax.swing.JTree;
import javax.swing.JMenuBar;
import javax.swing.JPopupMenu;
import java.awt.Component;
import java.awt.event.MouseAdapter;
import javax.swing.JMenu;
import javax.swing.JInternalFrame;
import javax.swing.BoxLayout;
import javax.swing.Box;
import javax.swing.tree.DefaultTreeModel;
import javax.swing.tree.DefaultMutableTreeNode;


public class MainWindow {

	private JFrame frmVineatoum;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					MainWindow window = new MainWindow();
					window.frmVineatoum.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public MainWindow() {
		initialize();
	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		frmVineatoum = new JFrame();
		frmVineatoum.setTitle("VineaToum");
		frmVineatoum.setBounds(100, 100, 810, 448);
		frmVineatoum.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		JMenuBar menuBar = new JMenuBar();
		frmVineatoum.getContentPane().add(menuBar, BorderLayout.NORTH);
		
		JMenu mnFichier = new JMenu("Fichier");
		menuBar.add(mnFichier);
		
		JDesktopPane desktopPane = new JDesktopPane();
		frmVineatoum.getContentPane().add(desktopPane, BorderLayout.CENTER);
		desktopPane.setLayout(new BorderLayout(0, 0));
		
		JToolBar toolBarMode = new JToolBar();
		desktopPane.add(toolBarMode, BorderLayout.WEST);
		toolBarMode.setOrientation(SwingConstants.VERTICAL);
		
		JButton btnCuverie = new JButton("Cuverie");
		toolBarMode.add(btnCuverie);
		
		JButton btnPlantation = new JButton("Plantation");
		toolBarMode.add(btnPlantation);
		
		JToolBar toolBarActions = new JToolBar();
		desktopPane.add(toolBarActions, BorderLayout.NORTH);
		
		JLabel lblNewLabel = new JLabel("New label");
		lblNewLabel.setVerticalAlignment(SwingConstants.BOTTOM);
		toolBarActions.add(lblNewLabel);
		
		JSplitPane splitPane = new JSplitPane();
		desktopPane.add(splitPane, BorderLayout.CENTER);
		
		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP);
		splitPane.setRightComponent(tabbedPane);
		
		PlantationPanel plantationPane = new PlantationPanel("Plantation");
		tabbedPane.addTab("Plantation", null, plantationPane, null);
		
		JScrollPane scrollPane_1 = new JScrollPane();
		splitPane.setLeftComponent(scrollPane_1);
		
		JTree tree = new JTree();
		tree.setModel(new DefaultTreeModel(
			new DefaultMutableTreeNode("JTree") {
				{
					DefaultMutableTreeNode node_1;
					node_1 = new DefaultMutableTreeNode("colors");
						node_1.add(new DefaultMutableTreeNode("blue"));
						node_1.add(new DefaultMutableTreeNode("violet"));
						node_1.add(new DefaultMutableTreeNode("red"));
						node_1.add(new DefaultMutableTreeNode("yellow"));
					add(node_1);
					node_1 = new DefaultMutableTreeNode("sports");
						node_1.add(new DefaultMutableTreeNode("basketball"));
						node_1.add(new DefaultMutableTreeNode("soccer"));
						node_1.add(new DefaultMutableTreeNode("football"));
						node_1.add(new DefaultMutableTreeNode("hockey"));
					add(node_1);
					node_1 = new DefaultMutableTreeNode("food");
						node_1.add(new DefaultMutableTreeNode("hot dogs"));
						node_1.add(new DefaultMutableTreeNode("pizza"));
						node_1.add(new DefaultMutableTreeNode("ravioli"));
						node_1.add(new DefaultMutableTreeNode("bananas"));
					add(node_1);
				}
			}
		));
		scrollPane_1.setViewportView(tree);
		/*
		Plantation plantationA = new Plantation("Plantation A");
		Plantation plantationB = new Plantation("Plantation B");
		Plantation plantationC = new Plantation("Plantation C");
		
		final String[] ids = new String[Plantation.getCollection().size()];
		int i = 0;
		for (Iterator iterator = Plantation.getCollection().iterator(); iterator.hasNext();) {
			Plantation p = (Plantation) iterator.next();
			ids[i] = p.getId();
			i++;
		}
		
		final JList listPlantation = new JList();
		listPlantation.setModel(new AbstractListModel() {
			String[] values = ids;
			
			public int getSize() {
				return values.length;
			}
			public Object getElementAt(int index) {
				return values[index];
			}
		});
		
		listPlantation.addListSelectionListener(new ListSelectionListener() {
			
			@Override
			public void valueChanged(ListSelectionEvent event) {
				System.out.println(listPlantation.getModel().getElementAt(event.getLastIndex()));
				//frame.getContentPane().removeAll();
				frame.getContentPane().add(new PlantationPanel((String) listPlantation.getModel().getElementAt(event.getLastIndex())), BorderLayout.EAST);
			}
		});
		
		
		//listPlantation.set
		frame.getContentPane().add(listPlantation, BorderLayout.CENTER);
		*/
	}
	private static void addPopup(Component component, final JPopupMenu popup) {
		component.addMouseListener(new MouseAdapter() {
			public void mousePressed(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			public void mouseReleased(MouseEvent e) {
				if (e.isPopupTrigger()) {
					showMenu(e);
				}
			}
			private void showMenu(MouseEvent e) {
				popup.show(e.getComponent(), e.getX(), e.getY());
			}
		});
	}
}
